package com.koron.css2.newProjectCenter.callback;

import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjPendingBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjPendingMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 工程挂起流程回调
 */
public class PjPendingCallback implements ProcessInterface {
    @Override
    public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
        try {
            if (operation == OPERATION_AGREE && fromNodeCode.equals("approval")) {
                PjPendingMapper pjPendingMapper = factory.getMapper(PjPendingMapper.class);
                PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
                PjPendingBean pjPending = pjPendingMapper.selectById(billId);
                PjInfoBean pjInfo = pjInfoMapper.selectById(pjPending.getPjId());
                pjInfo.setPendingState(PjPendingBean.PENDED_STATE); // 已挂起
                pjInfoMapper.update(pjInfo);

                pjPendingMapper.updatePendingState(pjPending.getId(), PjPendingBean.PENDED_STATE);

                logger.info("工程编号：{} 成功挂起", pjInfo.getBillNo());
            }
            if (operation == OPERATION_SUSPEND){
                PjPendingMapper pjPendingMapper = factory.getMapper(PjPendingMapper.class);
                PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

                PjPendingBean pjPending = pjPendingMapper.selectById(billId);
                PjInfoBean pjInfo = new PjInfoBean();
                pjInfo.setId(pjPending.getPjId());
                pjInfo.setDelayFlag(0);
                pjInfo.setTerminationFlag(0);
                pjInfo.setPendingState(0);
                pjInfoMapper.update(pjInfo);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程挂起工作流回调成功", null);
        } catch (Exception e) {
            logger.error("工程延期工作流回调失败", e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程挂起工作流回调失败", null);
    }

    @Override
    public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
        return null;
    }
}
